/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ToernooiWijzigen.java
 *
 * Created on 22-dec-2011, 11:34:58
 */
package view;
import javax.swing.JOptionPane;
import model.Toernooi;
/**
 *
 * @author tomkeim
 */
public class ToernooiWijzigen extends javax.swing.JFrame {
    protected int toernooiID = 0;
    /** Creates new form ToernooiWijzigen */
    public ToernooiWijzigen() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        zoekIDField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        datumField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        locatieField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        kostenField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        zoekButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Toernooi Wijzigen");

        jLabel1.setText("Zoeken (op ID):");

        jLabel2.setText("Datum:");

        jLabel3.setText("Locatie:");

        locatieField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locatieFieldActionPerformed(evt);
            }
        });

        jLabel4.setText("Kosten:");

        kostenField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kostenFieldActionPerformed(evt);
            }
        });

        saveButton.setText("Opslaan");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        zoekButton.setText("Zoeken");
        zoekButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zoekButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(saveButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(zoekIDField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(zoekButton))
                    .add(layout.createSequentialGroup()
                        .add(jLabel4)
                        .add(20, 20, 20)
                        .add(kostenField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel3))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(locatieField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
                            .add(datumField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(zoekIDField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(zoekButton))
                .add(9, 9, 9)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(datumField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(locatieField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(kostenField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(saveButton))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void locatieFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_locatieFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_locatieFieldActionPerformed

    private void kostenFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kostenFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_kostenFieldActionPerformed

    private void zoekButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zoekButtonActionPerformed
        toernooiID = 0;
        kostenField.setText("");
        locatieField.setText("");
        datumField.setText("");
        try {

                        toernooiID = Integer.parseInt(zoekIDField.getText());
                        Toernooi toernooi = Toernooi.getToernooi(toernooiID);

                        if(toernooi.getToernooiid() > 0) {
                                        kostenField.setText(""+toernooi.getKosten());
                                        locatieField.setText(toernooi.getLocatie());
                                        datumField.setText(""+toernooi.getDatumTijd());
                                    }

        } catch(NumberFormatException e) {
            JOptionPane.showMessageDialog(null,"Het toernooi id is geen geheel nummer. Vul een geheel nummer in.","Foutieve invoer.", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_zoekButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        try {
            if(!kostenField.getText().equals("")) {
                if(!datumField.getText().equals("")) {
                    if(!locatieField.getText().equals("")) {
                        if(toernooiID > 0) {
                        Toernooi toernooi = new Toernooi();
                        toernooi.setToernooiid(toernooiID);
                        toernooi.setDatumTijd(datumField.getText());
                        toernooi.setKosten(Double.parseDouble(kostenField.getText()));
                        toernooi.setLocatie(locatieField.getText());
                        if(toernooi.update() == true) {
                            JOptionPane.showMessageDialog(null,"Toernooi is gewijzigd!","Toernooi wijzigen", JOptionPane.INFORMATION_MESSAGE);
                            this.dispose();
                        } else {
                             JOptionPane.showMessageDialog(null,"Masterclass niet gewijzigd!.","Error", JOptionPane.ERROR_MESSAGE);

                        }
                        } else {
                          JOptionPane.showMessageDialog(null,"Zoek eerst naar masterclass!.","Error", JOptionPane.ERROR_MESSAGE);


                        }
                    }
                    else {
                         JOptionPane.showMessageDialog(null,"voer de locatie in voor een toernooi.");
                    }
                }
                else {
                     JOptionPane.showMessageDialog(null,"voer een datum in voor een toernooi.");
                }
            }
            else {
                JOptionPane.showMessageDialog(null,"voer de kosten in voor een toernooi.");
            }
        } catch(NumberFormatException e) {
            
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ToernooiWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ToernooiWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ToernooiWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ToernooiWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ToernooiWijzigen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField datumField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField kostenField;
    private javax.swing.JTextField locatieField;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton zoekButton;
    private javax.swing.JTextField zoekIDField;
    // End of variables declaration//GEN-END:variables
}
